package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.DistributorMessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.MarketingManagementConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.MarketingManagementBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.MartketingManagementFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

@EnableBinding(MarketingManagementConsumerBinder.class)
public class MarketingManagementListener {

    private static final Logger logger = LoggerFactory.getLogger(MarketingManagementListener.class);

    @Resource
    private MartketingManagementFeignClient martketingManagementFeignClient;

    @StreamListener(value = MarketingManagementBinderConstant.MARKETING_MANAGEMENT_INPUT)
    public void marketingOnline(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case MARKETING_ACTIVITY_MANAGEMENT_ONLINE_PUSH:
                    response = martketingManagementFeignClient.marketingActivityOnline(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: " + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("MarketingManagementListener.marketingActivityOnline RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("MarketingManagementListener.marketingActivityOnline Exception, type=" + type, e);
        }
    }

    @StreamListener(value = MarketingManagementBinderConstant.PROMOTION_MANAGEMENT_INPUT)
    public void promotionOnline(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case PROMOTION_ACTIVITY_MANAGEMENT_ONLINE_PUSH:
                    response = martketingManagementFeignClient.promotionActivityOnline(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: " + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("MarketingManagementListener.marketingActivityOnline RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("MarketingManagementListener.marketingActivityOnline Exception, type=" + type, e);
        }
    }

    @StreamListener(value = MarketingManagementBinderConstant.DISTRIBUTOR_MARKETING_MANAGEMENT_INPUT)
    public void distributorMarketingOnline(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_MARKETING_ACTIVITY_MANAGEMENT_ONLINE_PUSH:
                    response = martketingManagementFeignClient.marketingActivityOnline(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: " + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("MarketingManagementListener.marketingActivityOnline RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("MarketingManagementListener.marketingActivityOnline Exception, type=" + type, e);
        }
    }

    @StreamListener(value = MarketingManagementBinderConstant.DISTRIBUTOR_PROMOTION_MANAGEMENT_INPUT)
    public void distributorPromotionOnline(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_PROMOTION_ACTIVITY_MANAGEMENT_ONLINE_PUSH:
                    response = martketingManagementFeignClient.promotionActivityOnline(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: " + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("MarketingManagementListener.marketingActivityOnline RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("MarketingManagementListener.marketingActivityOnline Exception, type=" + type, e);
        }
    }

    @StreamListener(value = MarketingManagementBinderConstant.DISTRIBUTOR_COUPON_INPUT)
    public void distributorCoupon(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_COUPON_GET_PUSH:
                    response = martketingManagementFeignClient.couponGetPush(message);
                    break;
                case DISTRIBUTOR_COUPON_USE_PUSH:
                    response = martketingManagementFeignClient.couponUsePush(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: " + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("MarketingManagementListener.marketingActivityOnline RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("MarketingManagementListener.marketingActivityOnline Exception, type=" + type, e);
        }
    }


}
